package com.teacold.customdemo.chapter_eight;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import android.util.AttributeSet;
import android.view.View;

import com.teacold.customdemo.R;

public class AviodXfermodeView extends View {
    private Paint mPaint;
    private Bitmap mBmp;
    public AviodXfermodeView(Context context) {
        this(context,null);
    }

    public AviodXfermodeView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AviodXfermodeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint=new Paint();
        mPaint.setColor(Color.RED);
        mBmp=BitmapFactory.decodeResource(getResources(), R.drawable.dog);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width=getWidth()/2;
        int height=width*mBmp.getHeight()/mBmp.getWidth();
        int layerId=canvas.saveLayer(0,0,getWidth(),getHeight(),null,Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(mBmp,null,new Rect(0,0,width,height),mPaint);
//        mPaint.setXfermode(new AvoidXfermode(Color.WHITE,100,AvoidXfermode.Mode.TARGET));
        canvas.drawRect(0,0,width,height,mPaint);
        canvas.restoreToCount(layerId);
    }




}
